package com.kede.api.stand.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.kede.api.stand.dto.StoreProDuctInfoDTO;
import com.kede.api.stand.dto.StoreProductDTO;
import com.kede.api.stand.service.SupplieManageService;
import com.kede.api.stand.utils.GaoDeMapUtil;
import com.kede.domain.SuppliesDomain;
import com.kede.domain.SuppliesStoreDomain;
import com.kede.domain.ZhiUserCertifiedDomain;
import com.kede.pojo.Constant;
import com.kede.pojo.ResHttp;
import com.kede.service.StoreUserOrderService;
import com.kede.service.SuppliesService;
import com.kede.service.SuppliesStoreService;
import com.kede.service.ZhiUserCertifiedService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/***
 ** @Author:Pr.li
 ** @Description:
 ** @Date: create in 2023/6/7 12:02
 ***/
@Service
@Slf4j
public class SupplieServiceImpl implements SupplieManageService {

    @Resource
    private ZhiUserCertifiedService zhiUserCertifiedService;

    @Resource
    private SuppliesStoreService suppliesStoreService;

    @Resource
    private SuppliesService suppliesService;

    @Resource
    private StoreUserOrderService storeUserOrderService;

    private static final double MinCreateOderDistanceKM = 0.2;


    @Override
    public ResHttp getStoreSupple(String lng, String lat) {
//        GaoDeMapUtil.AddressComponentDto addressComponentByLocation = GaoDeMapUtil.getAddressComponentByLocation(lng, lat);
        //当前的地区编码
//        Integer adcode = addressComponentByLocation.getAdcode();
        List<ZhiUserCertifiedDomain> allNormalStore = zhiUserCertifiedService.findAllNormalStore();
        List<ZhiUserCertifiedDomain> collect = allNormalStore.stream().filter(certifiedDomain -> {
            Double v1 = GaoDeMapUtil.GetDistance(lng, lat, certifiedDomain.getLng(), certifiedDomain.getLat());
            if (v1 <= (certifiedDomain.getServiceDistance().doubleValue() / 1000  )) {
                LambdaQueryWrapper<SuppliesStoreDomain> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(SuppliesStoreDomain::getStoreId,certifiedDomain.getUserId());
                queryWrapper.gt(SuppliesStoreDomain::getCurrNum, Constant.ZERO);
                int count = suppliesStoreService.count(queryWrapper);
                certifiedDomain.setDistance(v1.toString());
                certifiedDomain.setCanCreatOder( v1 < MinCreateOderDistanceKM?1:0 );
                return count > 0;
            } else {
                return false;
            }
        }).collect(Collectors.toList());
        return ResHttp.okData(collect);
    }

    @Override
    public ResHttp getStoreProduct(Integer storeId,Integer userId) {

        try {
            LambdaQueryWrapper<SuppliesStoreDomain> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SuppliesStoreDomain::getStoreId,storeId);
            queryWrapper.gt(SuppliesStoreDomain::getCurrNum,Constant.ZERO);
            List<SuppliesStoreDomain> list = suppliesStoreService.list(queryWrapper);

            List<StoreProductDTO> storeProductDTOList = new ArrayList<>();
            for (SuppliesStoreDomain suppliesStoreDomain : list) {
                SuppliesDomain supplie = suppliesService.getById(suppliesStoreDomain.getSupplieId());
                int buyedNumBySupplieIdAndUserId = storeUserOrderService.getBuyedNumBySupplieIdAndUserId(supplie.getId(), userId);
//                if (buyedNumBySupplieIdAndUserId >= supplie.getBuyNum()){
//                    //如果用户购买的物资数量超过了 该物资的限制就不对此用户显示该商品
//                    continue;
//                }
                StoreProductDTO.StoreProductDTOBuilder storeProductDTOBuilder = StoreProductDTO.builder();
                StoreProductDTO storeProduct = storeProductDTOBuilder.img(supplie.getShowImg()).debi(supplie.getDebi()).proDuctName(supplie.getSupplieName())
                        .num(suppliesStoreDomain.getCurrNum()).buyedNum(buyedNumBySupplieIdAndUserId).money(supplie.getMoney())
                        .maxBuyNum(supplie.getBuyNum()).id(supplie.getId()).build();
                storeProductDTOList.add(storeProduct);
            }
            ZhiUserCertifiedDomain store = zhiUserCertifiedService.getOneByUserId(storeId);
            StoreProDuctInfoDTO build = StoreProDuctInfoDTO.builder().name(store.getStoreName()).storeProductDTOList(storeProductDTOList)
                    .addressDetail(store.getAddressDetaail()).endTime(store.getEndtime()).startTime(store.getStarttime())
                    .mobile(store.getMobile()).img(store.getZhizhaoimage()).build();
            return ResHttp.okData(build);
        }catch (Exception e){
            return ResHttp.fail();
        }
    }


    //分页啥的暂不考虑 现在要功能
    @Override
    public ResHttp getStoreProductByStore(Integer storeId) {
        try {
            LambdaQueryWrapper<SuppliesStoreDomain> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SuppliesStoreDomain::getStoreId,storeId);
            List<SuppliesStoreDomain> list = suppliesStoreService.list(queryWrapper);
            list.forEach(suppliesStoreDomain -> {
                SuppliesDomain supplie = suppliesService.getById(suppliesStoreDomain.getSupplieId());
                suppliesStoreDomain.setSupplies(supplie);
            });
            return ResHttp.okData(list);
        }catch (Exception e){
            return ResHttp.fail();
        }
    }

}
